<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Logic\Sms;
use App\Logic\Code;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\Models\Admin;


class RoleController extends Controller
{


    /**
     * @OA\Post(
     *     path="/admin/role_index",
     *     tags={"角色"},
     *     summary="列表",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function index()
    {

        $role_list = Db::table('role')->paginate();
        return s_msg('',$role_list);
    }

    /**
     * @OA\Post(
     *     path="/admin/role_xq",
     *     tags={"角色"},
     *     summary="详情",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"id"},
     *                 @OA\Property(property="id", type="number", description="角色id id"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function xq(Request $request)
    {
        if($request->has('id')){
            $role_info = Db::table('role')
                ->where('id',$request->input('id'))
                ->first();
            if($role_info){
                return s_msg('',$role_info);
            }else{
                return e_msg(300020,'角色信息不存在');
            }

        }else{
            return e_msg(300019,'未传入角色id id');
        }

    }
    /**
     * @OA\Post(
     *     path="/admin/role_add",
     *     tags={"角色"},
     *     summary="添加",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"name","synopsis","rules"},
     *                 @OA\Property(property="name", type="string", description="名字"),
     *                 @OA\Property(property="synopsis", type="string", description="简介"),
     *                 @OA\Property(property="rules", type="string", description="id集合，按逗号分割"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function add(Request $request)
    {

        $post_data = $request->all();

        $validateRule = [
            'name'                      => 'required',
            'synopsis'                      => 'required',
            'rules'                      => 'required',
        ];
        $errorMsg = [
            'name.required'                 => 'name名称不能为空',
            'synopsis.required'               => 'synopsis简介不能为空',
            'rules.required'                 => 'rules权限不能为空',
        ];

        $validator = Validator::make($post_data, $validateRule, $errorMsg);
        if ($validator->fails()){
            $errors = $validator->errors()->all();
            return e_msg(300001,$errors[0]);
        }

        $insert_arr = [
            'name' => $post_data['name'],
            'synopsis' => $post_data['synopsis'],
            'rules' => $post_data['rules'],
        ];
        $rel = DB::table('role')->insert($insert_arr);
        if($rel){
            return s_msg('添加成功');
        }else{
            return e_msg(300006,"请联系程序");
        }

    }

    /**
     * @OA\Post(
     *     path="/admin/role_edit",
     *     tags={"角色"},
     *     summary="修改",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                   required={"id","name","synopsis","rules"},
         *               @OA\Property(property="id", type="string", description="角色id id"),
         *               @OA\Property(property="name", type="string", description="名字"),
         *               @OA\Property(property="synopsis", type="string", description="简介"),
         *               @OA\Property(property="rules", type="string", description="id集合，按逗号分割"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function edit(Request $request)
    {
        $post_data = $request->all();

        $validateRule = [
            'id'                      => 'required',
            'name'                      => 'required',
            'synopsis'                      => 'required',
            'rules'                      => 'required',
        ];
        $errorMsg = [
            'id.required'                 => 'id 角色id不能为空',
            'name.required'                 => 'name名称不能为空',
            'synopsis.required'               => 'synopsis简介不能为空',
            'rules.required'                 => 'rules权限不能为空',
        ];

        $validator = Validator::make($post_data, $validateRule, $errorMsg);
        if ($validator->fails()){
            $errors = $validator->errors()->all();
            return e_msg(300001,$errors[0]);
        }

        $update_arr = [
            'name' => $post_data['name'],
            'synopsis' => $post_data['synopsis'],
            'rules' => $post_data['rules'],
        ];
        $rel = DB::table('role')->where('id',$post_data['id'])->update($update_arr);
        if($rel){
            return s_msg('修改成功');
        }else{
            return e_msg(300006,"您没有作出修改");
        }

    }

    /**
     * @OA\Post(
     *     path="/admin/role_delete",
     *     tags={"角色"},
     *     summary="删除",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"id"},
     *                 @OA\Property(property="id", type="number", description="角色id id"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function delete(Request $request)
    {
        if($request->has('id')){

            $role_info = Db::table('role')
                ->where('id',$request->input('id'))
                ->first();

            if($role_info){

                $adm = DB::table('admin')->where('role_id',$request->input('id'))->first();
                if($adm){
                    return e_msg(123456,'请删除相关账号或修改账号配置');
                }
                $rel = DB::table('role')->where('id',$request->input('id'))->delete();
                return s_msg('');
            }else{
                return e_msg(300020,'角色信息不存在');
            }

        }else{
            return e_msg(300019,'未传入角色id id');
        }

    }


    /**
     * @OA\Post(
     *     path="/admin/role_menu",
     *     tags={"角色"},
     *     summary="菜单列表",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function menu(Request $request)
    {

        $menu_list = Db::table('menu')->where('pid',0)->get();
        foreach ($menu_list as $key => $val){

            $children = DB::table('menu')->where('pid',$val->id)->get();
            foreach ($children as $k => $v){
                $children[$k]->children = DB::table('menu')->where('pid',$v->id)->get();
            }
            $menu_list[$key]->children = $children;

        }
        return s_msg('',$menu_list);

    }



}
